import { AddMobileContactData } from '@/utils/common-api.js'
export default {
  data() {
    return {
      id: '',
      dataObj: {},
      loadText: ['loadmore', 'loading', 'nomore'],
      loadTextIndex: 2,
      pageIndex: 1, //报价方列表 页码
      total: 0, //总数
      distanceSorting: 0, //距离排序1从近到远， 2从远到近，（没有传0）
      priceSorting: 0, //价格排序1从低到高， 2从高到低，（没有传0）
      lat: 0, //纬度
      lon: 0, //经度
      quotedList: [],
      sortDistance: false, //距离筛选
      sortPrice: false, //价格筛选
      pageFrom: '', //从哪里进入详情
    }
  },
  onLoad(option) {
    this.id = option.goodsid
    this.getPageDetail()
    this.location()
    this.GetBofferList()
    this.pageFrom = option.pageFrom ? option.pageFrom : ''
    console.log(this.$store.state)
  },
  methods: {
    //定位
    location() {
      uni.getLocation({
        type: 'gcj02',
        success: function (res) {
          this.lat = res.latitude
          this.lon = res.longitude
        },
      })
    },
    // 获取页面详情
    async getPageDetail() {
      try {
        const res = await this.$api.findCar.GetCarFindBDetails({ id: this.id })
        this.dataObj = res.data
      } catch (e) {
        //TODO handle the exception
      }
    },
    goDetail() {
      uni.navigateTo({
        url:
          '/pages_findCar/detailsForTheCar/detailsForTheCar?goodsid=' + this.id,
      })
    },
    gethideshow(phNum) {
      if (phNum) {
        return phNum.substr(0, 1) + '**'
      }
    },
    backTime(it, type) {
      if (it) {
        if (type === 1) {
          let month = it.split('T')[0].split('-')
          let time = month[1] + '月' + month[2] + '日 ' + it.split('T')[1]
          return time
        } else {
          return it.split('T')[0] + ' ' + it.split('T')[1]
        }
      }
    },
    // 跳转到店铺
    goStore(id) {
      uni.navigateTo({
        url: '/pages_common/store/store?id=' + id,
      })
    },
    // 获取报价列表
    async GetBofferList() {
      let data = {
        pageIndex: this.pageIndex,
        pageSize: 10,
        id: this.id,
        distanceSorting: this.distanceSorting,
        priceSorting: this.priceSorting,
        lat: this.lat,
        lon: this.lon,
      }
      try {
        const res = await this.$api.findCar.GetBofferList(data)
        this.quotedList = this.quotedList.concat(res.data.list)
        this.total = res.data.total
        console.log(res)
      } catch (e) {
        //TODO handle the exception
      }
    },
    //排序
    sort(type, status) {
      if (type === 1) {
        this.sortDistance = true
        this.distanceSorting = status
      } else {
        this.sortPrice = true
        this.priceSorting = status
      }
      this.quotedList = []
      this.GetBofferList()
    },
    // 取消排序
    cancelSort(type) {
      if (type === 1) {
        this.distanceSorting = 0
        this.sortDistance = false
      } else {
        this.sortPrice = true
        this.priceSorting = 0
      }
      this.quotedList = []
      this.GetBofferList()
    },
    // 发起交易
    goInitiatingContract(item) {
      console.log(this.dataObj, item, this.$store.state)
      let data = {
        seller: {
          id: item.operation_Account_Id,
          trueName: item.uname,
          mobile: item.phone,
          store_Name: item.store_Name,
          address: item.address,
          companyName: item.companyName,
        },
        sellstoreid: item.store_Id,
        buyers: {
          companyName: this.$store.state.userInfoDetail.store.companyName,
          store_Name: this.$store.state.userInfoDetail.store.storeName,
          trueName: this.$store.state.userInfoDetail.user.userName,
          id: this.$store.state.tid,
          mobile: this.$store.state.user.userInfo.mobile,
          address: this.dataObj.address,
        },
        buystoreid: this.$store.state.userInfoDetail.store.storeId,
        buyeruserid: this.$store.state.tid,
        maiState: 2,
        carMsg: {
          configulist: {
            id: this.dataObj.car_Id,
            factoryprice: this.dataObj.price,
            name: this.dataObj.car_name,
          },
          series: {
            carTypeName: this.dataObj.car_type_name,
          },
        },
        color: this.dataObj.facade_pictureList,
        goInPage: 'index',
        pageType: 'findcar',
        quotationMsg: {
          //报价信息
          deposit: item.deposit,
          service_Charge: item.service_Charge,
          service_ChargeName: item.service_ChargeName,
          car_Details: item.car_Details,
          car_DetailsName: item.car_DetailsName,
          produced_Date: item.produced_Date,
          findcarId: this.id,
        },
      }

      data.carMsg = JSON.stringify(data.carMsg)
      data.color.forEach((item) => {
        if (item.inColour) {
          item.inColour.neiColor = item.inColour.colorCoding
            ? item.inColour.colorCoding.split('/')
            : []
        }
        if (item.outerColour) {
          item.outerColour.neiColor = item.outerColour.colorCoding
            ? item.outerColour.colorCoding.split('/')
            : []
        }
      })
      data.color = JSON.stringify(data.color[0])
      console.log(data)
      uni.navigateTo({
        url:
          '/pages_order/initiatingContract/initiatingContract?data=' +
          encodeURIComponent(JSON.stringify(data)),
      })
    },
    // 拨打电话
    call(phone) {
      // console.log(this.pageDetail)
      AddMobileContactData(this.$store.state.userInfoDetail.store.storeId)
      uni.makePhoneCall({
        phoneNumber: phone, //仅为示例
      })
    },
    //联系商家
    contactSeller(data) {
      if (this.pageFrom === 'chat') {
        uni.navigateBack()
        return
      }
      this.$store.commit('news/createConversationActive', {
        user: data.store_Name, //店铺名称
        img: data.store_Topimg, //店铺头像
        lastTime: '', //
        userId: data.store_Id, //店铺id
        conversationID: 'C2C' + data.store_Id,
      })
      this.$store.commit('news/setCardInfo', {
        imgSrc: 'https://files.yzsheng.com/goodimg/brandimg/92.png',
        name:
          this.dataObj.branb_name +
          ' ' +
          this.dataObj.car_type_name +
          ' ' +
          this.dataObj.car_name,
        price: data.quoted_Price,
        labelPrice: this.dataObj.price,
        type: 'findCar',
      })
      uni.navigateTo({
        url:
          '/pages_news/contactSeller/contactSeller?user=' +
          data.store_Name +
          '&pageId=' +
          this.dataObj.id +
          '&tab=0',
      })
    },
  },

  onReachBottom() {
    let zs = Math.ceil(this.total / 10)
    if (zs > this.pageindex) {
      this.loadTextIndex = 1
      this.pageindex++
      this.getOrderList()
    } else {
      this.loadTextIndex = 2
    }
  },
}
